<?php

class ChisoController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        // content="text/plain; charset=utf-8"
        require_once ('jpgraph/src/jpgraph.php');
        require_once ('jpgraph/src/jpgraph_bar.php');
        require_once ('jpgraph/src/jpgraph_line.php');
        require_once ('jpgraph/src/jpgraph_pie.php');
        
        $front = Zend_Controller_Front::getInstance(); 
		$temp =$front->getParam('bootstrap')->getPluginResource('db');
		$option = $temp->getOptions(); 
		        
        mysql_connect($option['params']['host'], $option['params']['username'],$option['params']['password'])
         or die ("Could not connect to database"); 
        mysql_select_db($option['params']['dbname']) or die ("Could not select database");
    }
    
    public function preDispatch() 
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            if ($this->_request->getActionName() != 'login') {
                $this->_redirect('/user/login');
            }
        }
    }

    public function indexAction()
    {
        //Lay bang 1
        $vnindex = new Application_Model_IndexGrowth();
        $where = array( 'date = ?'   => '2011-01-21',
                        'ticker = ?' => 'VNINDEX'
        );
        $this->view->vnindex = $vnindex->fetchAll($where);
        
        $hnxindex = new Application_Model_IndexGrowth();
        $where = array( 'date = ?'   => '2011-01-21',
                        'ticker = ?' => 'HNXINDEX'
        );
        $this->view->hnxindex = $hnxindex->fetchAll($where);  
        
    }
    
    public function graph1Action()
    {
        $today = mysql_result(mysql_query("SELECT max(`date`) FROM `index_growth`"),0,0);
        $ten_day = date("Y-m-d",strtotime('-10 day',strtotime($today))) ;
        $count = mysql_result(mysql_query("SELECT COUNT(`close`) FROM `index_growth` WHERE `ticker`= 'VNINDEX' AND `date` >= '$ten_day' "),0,0);
        $sql = mysql_query("SELECT `close`,`volume` FROM `index_growth` WHERE `ticker`= 'VNINDEX' AND `date` >= '$ten_day' ");
        $data1 = array();
        $data2 = array();
        $close1 = array();
        $volume1 = array();
        for($i=0; $i < $count ; $i ++)
        {
            $close1[$i] = mysql_result($sql, $i, 0);
            $volume1[$i] = mysql_result($sql, $i, 1);
        }
        $data1 = $close1;
        $data2 = $volume1;
        // Create the graph. These two calls are always required
        $graph = new Graph(200,200);
        $graph->SetScale("textlin", 440, 540);
        $graph->SetY2Scale("lin",0,50000000);
        $graph->SetY2OrderBack(false);
        
        //$theme_class="DefaultTheme";
        //$graph->SetTheme(new $theme_class());
        
        // set major and minor tick positions manually
        //$graph->yaxis->SetTickPositions(array(440, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0), array(.5, 1.5, 2.5, 3.5, 4.5, 5.5));
        //$graph->y2axis->SetTickPositions(array(0, 0, 100, 200, 300));
        $graph->SetBox(false);
        
        //$graph->ygrid->SetColor('gray');
        $graph->ygrid->SetFill(false);
        //$graph->xaxis->SetTickLabels(array('A','B','C','D'));
        $graph->yaxis->HideLine(false);
        $graph->yaxis->HideTicks(false,false);
        
        // Create the bar plots
        $line1 = new LinePlot($data1);
        $p1 = new BarPlot($data2);
        
        // ...and add it to the graPH
        $graph->Add($line1);
        $graph->AddY2($p1);
        
        $p1->SetColor("green");
        $p1->SetLegend('Volume');
        $p1->SetFillGradient("green","green",GRAD_LEFT_REFLECTION);
        $p1->SetWidth(10);
        
        $line1->SetColor("red");
        $line1->SetLegend('VN Index');
        $line1->SetCenter();

        // Display the graph
        $graph->Stroke();
        $this->view->graph = $graph;   
    }
    
    public function graph2Action()
    {
        $today = mysql_result(mysql_query("SELECT max(`date`) FROM `index_growth`"),0,0);
        $ten_day = date("Y-m-d",strtotime('-10 day',strtotime($today))) ;
        $count = mysql_result(mysql_query("SELECT COUNT(`close`) FROM `index_growth` WHERE `ticker`= 'HNXINDEX' AND `date` >= '$ten_day' "),0,0);
        $sql = mysql_query("SELECT `close`,`volume` FROM `index_growth` WHERE `ticker`= 'HNXINDEX' AND `date` >= '$ten_day' ");
        $data1 = array();
        $data2 = array();
        $close1 = array();
        $volume1 = array();
        for($i=0; $i < $count ; $i ++)
        {
            $close1[$i] = mysql_result($sql, $i, 0);
            $volume1[$i] = mysql_result($sql, $i, 1);
        }
        $data1 = $close1;
        $data2 = $volume1;
        // Create the graph. These two calls are always required
        $graph = new Graph(200,200);
        $graph->SetScale("textlin", 102, 110);
        $graph->SetY2Scale("lin",0,50000000);
        $graph->SetY2OrderBack(false);
        
        $ten_day = date("Y-m-d",strtotime('-10 day',strtotime($today)));
        $a = mysql_query("SELECT COUNT(`close`) FROM `index_growth` WHERE `ticker`= 'VNINDEX' AND `date` >= '$ten_day' ");
        $sql = mysql_query("SELECT `close`,`volume` FROM `index_growth` WHERE `ticker`= 'VNINDEX' AND `date` >= '$ten_day' ");
        $count = mysql_result($a,0,0);
        //$theme_class="DefaultTheme";
        //$graph->SetTheme(new $theme_class());
        
        // set major and minor tick positions manually
        //$graph->yaxis->SetTickPositions(array(440, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0), array(.5, 1.5, 2.5, 3.5, 4.5, 5.5));
        //$graph->y2axis->SetTickPositions(array(0, 0, 100, 200, 300));
        $graph->SetBox(false);
        
        //$graph->ygrid->SetColor('gray');
        $graph->ygrid->SetFill(false);
        //$graph->xaxis->SetTickLabels(array('A','B','C','D'));
        $graph->yaxis->HideLine(false);
        $graph->yaxis->HideTicks(false,false);
        
        // Create the bar plots
        $line1 = new LinePlot($data1);
        $p1 = new BarPlot($data2);
        
        // ...and add it to the graPH
        $graph->Add($line1);
        $graph->AddY2($p1);
        
        $p1->SetColor("green");
        $p1->SetLegend('Volume');
        $p1->SetFillGradient("green","green",GRAD_LEFT_REFLECTION);
        $p1->SetWidth(10);
        
        $line1->SetColor("red");
        $line1->SetLegend('HNX Index');
        $line1->SetCenter();

        // Display the graph
        $graph->Stroke();
        $this->view->graph = $graph;           
    }
    public function graph3Action()
    {
        $sql = mysql_query("SELECT cr.`von_thi_truong`, cr.`stock_code` FROM `stock_date` st,`cal_risk` cr  WHERE cr.`stock_date_id` = st.`id` AND st.stock_exchange = 'HOSE'");
        $count = mysql_query("SELECT COUNT(cr.`von_thi_truong`) FROM `stock_date` st,`cal_risk` cr  WHERE cr.`stock_date_id` = st.`id` AND st.stock_exchange = 'HOSE'");
        $data = array();
        $stockcode = array();
        $n = mysql_result($count, 0, 0);
        for($i=0; $i<$n; $i++)
        {
            $data[$i] = mysql_result($sql, $i, 0);
            $stockcode[$i] = mysql_result($sql, $i, 1);
        }
        // Create the Pie Graph. 
        $graph = new PieGraph(410,550, "auto");
        $graph->SetShadow();
        
        $theme_class= new VividTheme;
        $graph->SetTheme($theme_class);
        
        // Set A title for the plot
        $graph->title->Set("XẾP HẠNG SỰ TÁC ĐỘNG LÊN CHỈ SỐ THEO VỐN HÓA SÀN GD TPHCM");
        
        // Create
        $name = array();
        $name[] = $stockcode;
        $p1 = new PiePlot($data);
        $p1->SetLegends($name[0]);
        $graph->Add($p1);
        $p1->value->show(false);
        $graph->Stroke();
        $this->view->graph = $graph;
    }
    
    public function graph4Action()
    {
        $sql = mysql_query("SELECT cr.`von_thi_truong`, cr.`stock_code` FROM `stock_date` st,`cal_risk` cr  WHERE cr.`stock_date_id` = st.`id` AND st.stock_exchange = 'HNX'");
        $count = mysql_query("SELECT COUNT(cr.`von_thi_truong`) FROM `stock_date` st,`cal_risk` cr  WHERE cr.`stock_date_id` = st.`id` AND st.stock_exchange = 'HNX'");
        $data = array();
        $stockcode = array();
        $n = mysql_result($count, 0, 0);
        for($i=0; $i<$n; $i++)
        {
            $data[$i] = mysql_result($sql, $i, 0);
            $stockcode[$i] = mysql_result($sql, $i, 1);
        }
        // Create the Pie Graph. 
        $graph = new PieGraph(410,550, "auto");
        $graph->SetShadow();
        
        $theme_class= new VividTheme;
        $graph->SetTheme($theme_class);
        
        // Set A title for the plot
        $graph->title->Set("XẾP HẠNG SỰ TÁC ĐỘNG LÊN CHỈ SỐ THEO VỐN HÓA SÀN GD HÀ NỘI");
        
        // Create
        $name = array();
        $name[] = $stockcode;
        $p1 = new PiePlot($data);
        $p1->SetLegends($name[0]);
        $graph->Add($p1);
        $p1->value->show(false);
        $graph->Stroke();
        $this->view->graph = $graph;
    }                   
}
